grApp.service('BaseService',['$http','$q',function($http,$q){
    var service = {};
    service.addToCart= function (pid,psid,num) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/product/addToCart',
            params:{pid:pid,psid:psid,num:num}
        }).success(function(data, status, headers, config) {
            deferred.resolve(data);
        }).error(function(data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.wxpay = function(trade_no,total_fee){
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http.post('/wx/pay', {trade_no:trade_no,total_fee:total_fee}).
            success(function(data, status, headers, config) {
                WeixinJSBridge.invoke('getBrandWCPayRequest',data,
                    function(res){
                        if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                            deferred.resolve(res);
                        }else{
                            deferred.reject(res);
                        }
                    }
                );
            }).
            error(function(data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    //签到Ajax操作
    service.setSignDayAction = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/activity/setSignDayActionAjax'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).
            error(function (data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    //进入页面获取数据
    service.getSignDayValue = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/activity/getSignDayValueAjax'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).
            error(function (data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    //领取奖品操作
    service.getGifts=function(level){
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/activity/getGift',
            params: {level: level}
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).
            error(function (data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    return service;
}]);
